package com.tsmti.activiti.listener;

/**
 * @Package：com.tsmti.activiti.listener
 * @Class：AutoCompleteFirstTaskEventListener
 * @Description： 自动提交第一个创建节点
 * @Author：zcw
 * @Date：Created in 2018/9/13 21:52
 * @Company:
 * @Version：
 * @Modified By:
 */

import com.tsmti.core.common.constant.Constants;
import com.tsmti.core.util.SpringContextUtils;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.impl.ActivitiEntityEventImpl;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.pvm.PvmActivity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;

public class AutoCompleteFirstTaskEventListener implements ActivitiEventListener {
    @Override
    public void onEvent(ActivitiEvent activitiEvent) {
        if (!(activitiEvent instanceof ActivitiEntityEventImpl)) {
            return;
        }

        ActivitiEntityEventImpl activitiEntityEventImpl = (ActivitiEntityEventImpl) activitiEvent;
        Object entity = activitiEntityEventImpl.getEntity();

        if (!(entity instanceof TaskEntity)) {
            return;
        }

        TaskEntity taskEntity = (TaskEntity) entity;

        try {
            switch (activitiEvent.getType()) {
                case TASK_CREATED:
                    this.onCreate(taskEntity);
                    break;
                default:
            }
        } catch (Exception ex) {
        }
    }

    private void onCreate(DelegateTask delegateTask) throws Exception {
        //如果是流程的第一步，则自动提交
        PvmActivity targetActivity = findFirstActivity(delegateTask.getProcessDefinitionId());

        if (!targetActivity.getId().equals(delegateTask.getExecution().getCurrentActivityId())) {
            return;
        }

        new AutoCompleteCmd(delegateTask.getId(),delegateTask.getVariables(),"发起流程").execute(Context.getCommandContext());
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

    public RepositoryService getRepositoryService(){
        return SpringContextUtils.getBean(RepositoryService.class);
    }

    public PvmActivity findFirstActivity(String processDefinitionId) {
        ProcessDefinitionEntity processDefinitionEntity  = (ProcessDefinitionEntity)
                ((RepositoryServiceImpl)getRepositoryService())
                        .getDeployedProcessDefinition(processDefinitionId);

        ActivityImpl startActivity = processDefinitionEntity.getInitial();

        PvmTransition pvmTransition = startActivity.getOutgoingTransitions().get(0);
        PvmActivity targetActivity = pvmTransition.getDestination();

        if (!Constants.USER_TASK.equals(targetActivity.getProperty(Constants.TYPE))) {
            return null;
        }
        return targetActivity;
    }
}